﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

namespace ExtendedSplash
{
    sealed partial class App : Application
    {
        public App()
        {
            this.InitializeComponent();
        }

        protected Frame Frame
        {
            get
            {
                Frame rootFrame = Window.Current.Content as Frame;
                if (rootFrame == null)
                {
                    rootFrame = new Frame();
                    Window.Current.Content = rootFrame;
                }
                return rootFrame;
            }
        }

        protected override void OnLaunched(LaunchActivatedEventArgs args) { Activate(args as IActivatedEventArgs); }
        protected override void OnActivated(IActivatedEventArgs args) { Activate(args as IActivatedEventArgs); }
        protected override void OnSearchActivated(SearchActivatedEventArgs args) { Activate(args as IActivatedEventArgs); }
        public void Activate(IActivatedEventArgs e)
        {
            if (e.PreviousExecutionState == ApplicationExecutionState.Running)
                Frame.Navigate(typeof(MainPage));
            else
            {
                // do not activate, let the splash activate itself
                var splash = new Views.Splash()
                {
                    SplashScreen = e.SplashScreen,
                    Navigate = () => { Frame.Navigate(typeof(MainPage)); }
                };
                Window.Current.Content = splash as UIElement;
                splash.Start();
            }
        }
    }
}
